package com.lin.views;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


import com.lin.bean.PingLunBean;
import com.lin.bean.ZanList;
import com.lin.utils.Parameters;
import com.ly.quickdev.library.adapter.ListBaseAdapter;
import com.ly.quickdev.library.view.SquareImageView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.PinterestCircleBitmapDisplayer;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.yunfeng.supercall.R;
import com.yunfeng.supercall.app.AppContext;
import com.yunfeng.supercall.bean.FriendsShareBean;
import com.yunfeng.supercall.bean.FriendsShareBean;
import com.yunfeng.supercall.bean.SuperFriendsBean;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ImageView.ScaleType;

public class ListViewForFriendShare extends ListView{
	SimpleDateFormat mSimpleDateFormat;
	SimpleDateFormat mSimpleDateFormat1;
	MyAdapter mFocusGameAdapter;
	Context mContext;
	DisplayImageOptions options;
	DisplayImageOptions optionsCircle;
	OnItemListViewEventListener mOnItemListViewEventListener;

	public ListViewForFriendShare(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		mSimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		mSimpleDateFormat1 = new SimpleDateFormat("yyyy/MM/dd HH:mm");
		mContext = context;
		options = new DisplayImageOptions.Builder()
		.displayer(new RoundedBitmapDisplayer(context.getResources().getDimensionPixelOffset(R.dimen.corners_avatar)))
		.showImageOnFail(R.drawable.default_avatar)
		.showImageOnLoading(R.drawable.default_avatar)
		.cacheInMemory(true)
		.cacheOnDisk(true)
		.considerExifParams(true)
		.bitmapConfig(Bitmap.Config.RGB_565)
		.build();

		optionsCircle = new DisplayImageOptions.Builder()
		/*			.showImageOnFail(R.drawable.default_avatar)
		.showImageOnLoading(R.drawable.default_avatar)*/
		.displayer(new PinterestCircleBitmapDisplayer(20))
		.cacheInMemory(true)
		.cacheOnDisk(true)
		.considerExifParams(true)
		.bitmapConfig(Bitmap.Config.RGB_565)
		.build();

	}

	public void setOnItemListViewEventListener(OnItemListViewEventListener mOnItemListViewEventListener) {
		this.mOnItemListViewEventListener = mOnItemListViewEventListener;
	}

	public interface OnItemListViewEventListener {
		public void onItemAvatarLisener(FriendsShareBean maListInfo, int x, int y);
		public void onItemClickListener(FriendsShareBean maListInfo, int x, int y);
		public void onItemUserDetailListener(FriendsShareBean maListInfo, int x, int y);
		public void onItemGridViewClickListener(FriendsShareBean maListInfo, int position);
		public void onItemProClickListener(FriendsShareBean maListInfo, int x, int y);
	}


	public void setDataList(ArrayList<FriendsShareBean> mFocusGameList) {
		if (mFocusGameAdapter == null) {
			mFocusGameAdapter = new MyAdapter(getContext(), mFocusGameList);
			setAdapter(mFocusGameAdapter);
		} else {
			mFocusGameAdapter.setmFocusGameList(mFocusGameList);
			mFocusGameAdapter.notifyDataSetChanged();
		}
	}

	private class MyAdapter extends BaseAdapter implements OnClickListener, OnItemClickListener{

		LayoutInflater mLayoutInflater;
		ArrayList<FriendsShareBean> mDataList;
		int mSize;

		public MyAdapter(Context context,
				ArrayList<FriendsShareBean> mDataList) {
			// TODO Auto-generated constructor stub
			this.mDataList = mDataList;
			mLayoutInflater =  LayoutInflater.from(mContext);
		}

		public ArrayList<FriendsShareBean> getDataList() {
			return mDataList;
		}

		public void setmFocusGameList(ArrayList<FriendsShareBean> mFocusGameList) {
			this.mDataList = mFocusGameList;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			try {
				mSize = mDataList.size();
			} catch(NullPointerException e) {
				mSize = 0;
			}
			return mSize;
		}


		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}

		@Override
		public FriendsShareBean getItem(int position) {
			// TODO Auto-generated method stub
			return mDataList.get(position);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup arg2) {
			// TODO Auto-generated method stub
			ViewHolder viewholder = null;

			int type = getItemViewType(position);
			if (convertView == null) {
				viewholder = new ViewHolder();
				convertView = mLayoutInflater.inflate(
						R.layout.list_item_friends_share, null);
				viewholder.ivAvatar = (ImageView) convertView.findViewById(R.id.avatar);
				viewholder.tvUsername = (TextView) convertView.findViewById(R.id.user_name);
				viewholder.tvDate = (TextView) convertView.findViewById(R.id.date);
				viewholder.tvContent = (TextView) convertView.findViewById(R.id.content);
				viewholder.gridview = (GridView) convertView.findViewById(R.id.gridview);
				viewholder.imageview1 = (ImageView) convertView.findViewById(R.id.imageview1);
				viewholder.imageview2 = (ImageView) convertView.findViewById(R.id.imageview2);
				viewholder.imageview3 = (ImageView) convertView.findViewById(R.id.imageview3);
				viewholder.linearLayout= (LinearLayout) convertView.findViewById(R.id.linearlayout);
				//				viewholder.gridviewUser = (GridView) convertView.findViewById(R.id.gridview_users);

								viewholder.imageviewPro = (ImageView) convertView.findViewById(R.id.imageview_pro); 
				viewholder.tvZanCount = (TextView) convertView.findViewById(R.id.zan_count);
				viewholder.listview = (GridView) convertView.findViewById(R.id.listview);
				convertView.setTag(viewholder);

			} else {
				viewholder = (ViewHolder) convertView.getTag();
			}

			FriendsShareBean friendsShare = getItem(position);

			viewholder.tvContent.setText(friendsShare.getContent());
			viewholder.tvZanCount.setText(friendsShare.getZannum());
			viewholder.tvUsername.setText(friendsShare.getNick_name());
			viewholder.tvDate.setText(friendsShare.getCreatetime());
			viewholder.gridview.setAdapter(new GridImgAdapter2(mContext, friendsShare
					.getPhotoList()));
			//			viewholder.gridviewUser.setAdapter(new GridImgAdapter3(mContext, friendsShare
			//					.getPhotoList()));

			viewholder.gridview.setTag(friendsShare);

			viewholder.gridview.setOnItemClickListener(this);
			viewholder.listview.setAdapter(new GridCommentAdapter(mContext, friendsShare.getJsonPinglunList()));


			ImageLoader.getInstance().displayImage(Parameters.SERVER_PIC_URL + friendsShare.getPhoto(), viewholder.ivAvatar, options);

			ArrayList<ZanList> mListZan = friendsShare.getmZanList();
			if(mListZan != null &&mListZan.size() > 0) {
				if(mListZan.size() == 1) {
					ImageLoader.getInstance().displayImage(Parameters.SERVER_PIC_URL + mListZan.get(0).getPhoto(), viewholder.imageview1, optionsCircle);
					viewholder.imageview1.setVisibility(View.VISIBLE);
					viewholder.imageview2.setVisibility(View.GONE);
					viewholder.imageview3.setVisibility(View.GONE);
				} else if(mListZan.size() == 2) {
					ImageLoader.getInstance().displayImage(Parameters.SERVER_PIC_URL + mListZan.get(0).getPhoto(), viewholder.imageview2, optionsCircle);
					ImageLoader.getInstance().displayImage(Parameters.SERVER_PIC_URL + mListZan.get(1).getPhoto(), viewholder.imageview3, optionsCircle);
					viewholder.imageview1.setVisibility(View.VISIBLE);
					viewholder.imageview2.setVisibility(View.VISIBLE);
					viewholder.imageview3.setVisibility(View.GONE);
				} else if(mListZan.size() == 3) {
					ImageLoader.getInstance().displayImage(Parameters.SERVER_PIC_URL + mListZan.get(0).getPhoto(), viewholder.imageview1, optionsCircle);
					ImageLoader.getInstance().displayImage(Parameters.SERVER_PIC_URL + mListZan.get(1).getPhoto(), viewholder.imageview2, optionsCircle);
					ImageLoader.getInstance().displayImage(Parameters.SERVER_PIC_URL + mListZan.get(2).getPhoto(), viewholder.imageview3, optionsCircle);
					viewholder.imageview1.setVisibility(View.VISIBLE);
					viewholder.imageview2.setVisibility(View.VISIBLE);
					viewholder.imageview3.setVisibility(View.VISIBLE);
				}
			} else {
				viewholder.imageview1.setVisibility(View.GONE);
				viewholder.imageview2.setVisibility(View.GONE);
				viewholder.imageview3.setVisibility(View.GONE);
			}
			
			FriendsShareBean base = friendsShare;
			viewholder.tvUsername.setText(base.getNick_name());
			ImageLoader.getInstance().displayImage(Parameters.SERVER_PIC_URL + base.getPhoto(), viewholder.ivAvatar, options);
			viewholder.ivAvatar.setOnClickListener(this);
			viewholder.linearLayout.setOnClickListener(this);
			viewholder.imageviewPro.setOnClickListener(this);
			viewholder.imageviewPro.setTag(base);
			viewholder.ivAvatar.setTag(base);
			long date = 0;
			try {
				date = mSimpleDateFormat1.parse(base.getCreatetime()).getTime();
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
//			viewholder.tvDate.setText(generateTime(date));
			// long date = new cale
			viewholder.linearLayout.setTag(base);

			//			viewholder.imageviewPro.setOnClickListener(this);
			//			viewholder.imageviewPro.setTag(friendsShare);
			return convertView;

		}




		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			int[] mLocation = new int[2];
			v.getLocationInWindow(mLocation);
			FriendsShareBean mDownloadListInfo = (FriendsShareBean) v.getTag();
			if(mOnItemListViewEventListener != null) { // if we don't define this , there'll be the null pointer exception when we click an item if not setOnXXListener not work
				if(v.getId() == R.id.avatar) {
					mOnItemListViewEventListener.onItemAvatarLisener(mDownloadListInfo, mLocation[0], mLocation[1]);
				} else if(v.getId() == R.id.linearlayout) {
					mOnItemListViewEventListener.onItemClickListener(mDownloadListInfo, mLocation[0], mLocation[1]);
				} else if(v.getId() == R.id.is_super) {
					mOnItemListViewEventListener.onItemUserDetailListener(mDownloadListInfo, mLocation[0], mLocation[1]);
				} else if(v.getId() == R.id.imageview_pro) {
					mOnItemListViewEventListener.onItemProClickListener(mDownloadListInfo, mLocation[0], mLocation[1]);
				}
			}

		}

		@Override
		public void onItemClick(AdapterView<?> v, View arg1, int position,
				long arg3) {
			// TODO Auto-generated method stub
			FriendsShareBean mDownloadListInfo = (FriendsShareBean) v.getTag();
			if(mOnItemListViewEventListener != null) { // if we don't define this , there'll be the null pointer exception when we click an item if not setOnXXListener not work
				mOnItemListViewEventListener.onItemGridViewClickListener(mDownloadListInfo, position);
			}

		}
	}

	public String generateTime(long date) {

		Date date2 = new Date(date);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date2);
		Calendar now = Calendar.getInstance();
		int count = now.get(Calendar.DAY_OF_YEAR)
				- calendar.get(Calendar.DAY_OF_YEAR);
		if (count == 1) {
			return "昨天 " + calendar.get(Calendar.HOUR_OF_DAY) + ":"
					+ calendar.get(Calendar.MINUTE);
		} else if (count == 0) {
			int minu = calendar.get(Calendar.MINUTE);
			return calendar.get(Calendar.HOUR_OF_DAY) + ":"
			+ (minu < 10 ? "0" : "") + minu;
		}
		return mSimpleDateFormat.format(date2);

	}


	static class ViewHolder {
		ImageView ivAvatar;
				ImageView imageviewPro;
		TextView tvUsername;
		TextView tvDate;
		GridView gridview;
		//		GridView gridviewUser;
		TextView tvContent;
		TextView tvZanCount;
		ImageView isSuper;
		LinearLayout linearLayout;
		GridView listview;
		ImageView imageview1;
		ImageView imageview2;
		ImageView imageview3;
	}

	public static class GridImgAdapter2 extends BaseAdapter {
		Context context;
		LayoutInflater mLayoutInflater;
		List<String> mDataList;
		DisplayImageOptions options;
		public GridImgAdapter2(Context context,
				List<String> list) {
			// TODO Auto-generated constructor stub
			this.mDataList = list;
			this.context = context;

			mLayoutInflater =  LayoutInflater.from(context);
			options = new DisplayImageOptions.Builder()
			/*			.showImageOnFail(R.drawable.default_avatar)
			.showImageOnLoading(R.drawable.default_avatar)*/
			.cacheInMemory(true)
			.cacheOnDisk(true)
			.considerExifParams(true)
			.bitmapConfig(Bitmap.Config.RGB_565)
			.build();
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mDataList.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return mDataList.get(position);
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup arg2) {
			// TODO Auto-generated method stub

			if (convertView == null) {
				SquareImageView imageView = new SquareImageView(context);
				imageView.setScaleType(ScaleType.CENTER_CROP);
				convertView = imageView;
				ImageLoader.getInstance().displayImage(Parameters.SERVER_PIC_URL + mDataList.get(position), imageView, options);
			}
			return convertView;
		}




	}


	public static class GridImgAdapter3 extends BaseAdapter {
		Context context;
		LayoutInflater mLayoutInflater;
		List<String> mDataList;
		DisplayImageOptions options;
		public GridImgAdapter3(Context context,
				List<String> list) {
			// TODO Auto-generated constructor stub
			this.mDataList = list;
			this.context = context;

			mLayoutInflater =  LayoutInflater.from(context);
			options = new DisplayImageOptions.Builder()
			/*			.showImageOnFail(R.drawable.default_avatar)
			.showImageOnLoading(R.drawable.default_avatar)*/
			.displayer(new PinterestCircleBitmapDisplayer(20))
			.cacheInMemory(true)
			.cacheOnDisk(true)
			.considerExifParams(true)
			.bitmapConfig(Bitmap.Config.RGB_565)
			.build();
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mDataList.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return mDataList.get(position);
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup arg2) {
			// TODO Auto-generated method stub

			if (convertView == null) {
				convertView = mLayoutInflater.inflate(R.layout.list_item_user_zhan, null);

			}
			ImageView imageview = (ImageView) convertView.findViewById(R.id.imageview);
			ImageLoader.getInstance().displayImage(Parameters.SERVER_PIC_URL + mDataList.get(position), imageview, options);
			return convertView;

		}

	}

	public static class GridCommentAdapter extends BaseAdapter {
		Context context;
		LayoutInflater mLayoutInflater;
		ArrayList<PingLunBean> mDataList;
		DisplayImageOptions options;
		private TextView textview;
		private ImageView imageViewAvatar;
		private TextView textviewAuthor;
		public GridCommentAdapter(Context context,
				ArrayList<PingLunBean> list) {
			// TODO Auto-generated constructor stub
			this.mDataList = list;
			this.context = context;

			mLayoutInflater =  LayoutInflater.from(context);
			options = new DisplayImageOptions.Builder()
			/*			.showImageOnFail(R.drawable.default_avatar)
			.showImageOnLoading(R.drawable.default_avatar)*/
			.displayer(new PinterestCircleBitmapDisplayer(20))
			.cacheInMemory(true)
			.cacheOnDisk(true)
			.considerExifParams(true)
			.bitmapConfig(Bitmap.Config.RGB_565)
			.build();
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			if(mDataList !=null) {
				return mDataList.size();
			} else {
				return 0;
			}
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return mDataList.get(position);
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup arg2) {
			// TODO Auto-generated method stub

			if (convertView == null) {
				convertView = mLayoutInflater.inflate(R.layout.list_item_user_comment, null);
				textview = (TextView) convertView.findViewById(R.id.textview);
				textviewAuthor = (TextView) convertView.findViewById(R.id.textview_author);
				
				imageViewAvatar = (ImageView) convertView.findViewById(R.id.imageview_avatar);
			}
			ImageLoader.getInstance().displayImage(Parameters.SERVER_PIC_URL + mDataList.get(position).getPhoto(), imageViewAvatar, options);

			
			textviewAuthor.setText(mDataList.get(position).getNick_name() );
			textview.setText(":" + mDataList.get(position).getContent());

			return convertView;
		}

	}
}